package com.marketingcenter.controller;

import com.marketingcenter.entity.DiscountCard;
import com.marketingcenter.service.DiscountCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 折扣卡控制器
 */
@RestController
@RequestMapping("/discount-cards")
public class DiscountCardController {

    @Autowired
    private DiscountCardService discountCardService;

    /**
     * 创建折扣卡
     */
    @PostMapping
    public ResponseEntity<DiscountCard> create(@RequestBody DiscountCard discountCard) {
        discountCard.setType(DiscountCard.PromotionType.DISCOUNT_CARD);
        DiscountCard createdCard = discountCardService.create(discountCard);
        return ResponseEntity.ok(createdCard);
    }

    /**
     * 更新折扣卡
     */
    @PutMapping("/{id}")
    public ResponseEntity<DiscountCard> update(@PathVariable Long id, @RequestBody DiscountCard discountCard) {
        discountCard.setId(id);
        DiscountCard updatedCard = discountCardService.update(discountCard);
        return ResponseEntity.ok(updatedCard);
    }

    /**
     * 删除折扣卡
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        discountCardService.delete(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID查询折扣卡
     */
    @GetMapping("/{id}")
    public ResponseEntity<DiscountCard> getById(@PathVariable Long id) {
        DiscountCard card = discountCardService.findById(id);
        return card != null ? ResponseEntity.ok(card) : ResponseEntity.notFound().build();
    }

    /**
     * 查询所有折扣卡
     */
    @GetMapping
    public ResponseEntity<List<DiscountCard>> getAll() {
        List<DiscountCard> cards = discountCardService.findAll();
        return ResponseEntity.ok(cards);
    }

    /**
     * 根据状态查询折扣卡
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<DiscountCard>> getByStatus(@PathVariable Integer status) {
        List<DiscountCard> cards = discountCardService.findByStatus(status);
        return ResponseEntity.ok(cards);
    }

    /**
     * 查询活动中的折扣卡
     */
    @GetMapping("/active")
    public ResponseEntity<List<DiscountCard>> getActive() {
        List<DiscountCard> cards = discountCardService.findActivePromotions();
        return ResponseEntity.ok(cards);
    }

    /**
     * 启用折扣卡
     */
    @PutMapping("/{id}/enable")
    public ResponseEntity<Void> enable(@PathVariable Long id) {
        discountCardService.enable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 停用折扣卡
     */
    @PutMapping("/{id}/disable")
    public ResponseEntity<Void> disable(@PathVariable Long id) {
        discountCardService.disable(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 领取折扣卡
     */
    @PostMapping("/{id}/receive")
    public ResponseEntity<Boolean> receive(@PathVariable Long id, @RequestParam Long userId) {
        boolean success = discountCardService.distributeDiscountCard(id, userId);
        return ResponseEntity.ok(success);
    }

    /**
     * 使用折扣卡
     */
    @PostMapping("/{id}/use")
    public ResponseEntity<Boolean> useDiscountCard(@PathVariable Long id, @RequestParam Long userId, @RequestParam Long orderId) {
        boolean success = discountCardService.useDiscountCard(id, userId, orderId);
        return ResponseEntity.ok(success);
    }

}